﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Oracle.DataAccess.Client;
using System.Data;

namespace Wicresoft.fjyc.Integration.HR.Interface
{
    public static class SyncDateHelper
    {
        private readonly static string constr = ConfigurationManager.ConnectionStrings["UnifyShowConStr"].ConnectionString;
        //
        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectName">ITSM："ITSMSycEmployee" 自服务："SelfSycEmployee"  统一展现："UnifyShowSycEmployee"  </param>
        /// <returns></returns>
        public static DateTime GetStartDate(string projectName)
        {
            using (OracleConnection conn = new OracleConnection(constr))
            {
                conn.Open();
                string selectStr = "select endtime from SYNCTIMES where kind =:projectName";
                OracleCommand oc = conn.CreateCommand();
                oc.CommandText = selectStr;
                oc.CommandType = CommandType.Text;
                oc.BindByName = true;
                oc.Parameters.Add(new OracleParameter()
                {
                    ParameterName = "projectName",
                    OracleDbType = OracleDbType.NVarchar2,
                    Size = 40,
                    Value = projectName
                });
                using (OracleDataReader odr = oc.ExecuteReader())
                {
                    odr.Read();
                    return (DateTime)odr["endtime"];
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="projectName">ITSM："ITSMSycEmployee" 自服务："SelfSycEmployee"  统一展现："UnifyShowSycEmployee"  </param>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static object PushEndDate(string projectName, DateTimePeroid dt)
        {
            using (OracleConnection conn = new OracleConnection(constr))
            {
                conn.Open();
                string selectStr = "UPDATE  padmin.synctimes starttime = :start,endtime=:end WHERE kind=:projectName";
                OracleCommand oc = conn.CreateCommand();
                oc.CommandText = selectStr;
                oc.CommandType = CommandType.Text;
                oc.BindByName = true;
                oc.Parameters.Add(new OracleParameter()
                {
                    ParameterName = "start",
                    OracleDbType = OracleDbType.Date,
                    Value = dt.Start
                });
                oc.Parameters.Add(new OracleParameter()
                {
                    ParameterName = "end",
                    OracleDbType = OracleDbType.Date,
                    Value = dt.End
                });
                oc.Parameters.Add(new OracleParameter()
                {
                    ParameterName = "projectName",
                    OracleDbType = OracleDbType.NVarchar2,
                    Size = 40,
                    Value = projectName
                });
                return oc.ExecuteScalar();
            }
        }



    }
}
